package in.iqing.control.api.callback;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;

import in.iqing.control.helper.LogHelper;
import in.iqing.model.bean.AuthResult;
import in.iqing.model.bean.User;

/**
 * Created by ZuoShu on 15/7/7.
 */
public abstract class RegisterCallback extends BaseCallback {
    public abstract void onSuccess(User user);

    @Override
    public void onSuccess(String data) {
        if (TextUtils.isEmpty(data)) {
            onFailure(ERROR_CODE_UNKNOWN, "");
            return;
        }

        User user = JSON.parseObject(data, User.class);
        if (user != null && !TextUtils.isEmpty(user.getUsername())) {
            onSuccess(user);
        } else {
            AuthResult authResult = JSON.parseObject(data, AuthResult.class);
            onFailure(authResult.getCode(), "");
            LogHelper.warn("RegisterCallback", "Register fail result:" + authResult.toString());
        }
    }

    @Override
    public void onFailure(int code, String msg) {
    }
}
